package com.baidu.infinity.ui.fragment.home.view

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager.LayoutParams
import android.widget.PopupWindow
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.baidu.infinity.R
import com.baidu.infinity.databinding.LayoutImageShowViewBinding
import com.baidu.infinity.databinding.LayoutLayerItemBinding
import com.baidu.infinity.databinding.LayoutLoadingAnimBinding
import com.baidu.infinity.model.ImageModel
import com.baidu.infinity.model.LayerModel
import com.baidu.infinity.ui.fragment.home.shape.LayerManager
import com.baidu.infinity.ui.util.ImageModelManager
import com.baidu.infinity.ui.util.LayerModelManager
import com.baidu.infinity.ui.util.state.ImageState
import com.baidu.infinity.ui.util.state.LayerState
import com.bumptech.glide.Glide
import com.drake.brv.BindingAdapter
import com.drake.brv.listener.DefaultItemTouchCallback
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import androidx.core.graphics.drawable.toDrawable

class LoadAnimPopUpWindow(context: Context) {
    var mBinding: LayoutLoadingAnimBinding ?= null
    val layoutInflater = LayoutInflater.from(context)
    val popUpWindow: PopupWindow by lazy {
        mBinding = LayoutLoadingAnimBinding.inflate(layoutInflater)
        PopupWindow(context).apply {
            contentView = mBinding!!.root
            width = LayoutParams.WRAP_CONTENT
            height = LayoutParams.WRAP_CONTENT
            setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
        }
    }
    fun showAtLocation(parent: View,gravity: Int = Gravity.CENTER, x: Int = 0, y: Int = 0){
        popUpWindow.showAtLocation(parent,gravity,x, y)
        playLoadingAnim()

    }
    fun showAsDropDown(anchor: View, xoff: Int, yoff: Int, gravity: Int){
        popUpWindow.showAsDropDown(anchor,xoff, yoff, gravity)
        playLoadingAnim()
    }
    fun hide(){
        popUpWindow.dismiss()
    }
    fun playLoadingAnim(){
        mBinding!!.load.visibility = View.VISIBLE
        mBinding!!.ok.visibility = View.INVISIBLE
        mBinding!!.load.playAnimation()

    }
    fun playOkAnim(){
        mBinding!!.load.visibility = View.INVISIBLE
        mBinding!!.ok.visibility = View.VISIBLE
        mBinding!!.ok.playAnimation()
        mBinding!!.ok.addAnimatorListener(object : AnimatorListener{
            override fun onAnimationStart(animation: Animator) {}

            override fun onAnimationEnd(animation: Animator) {
                hide()
            }

            override fun onAnimationCancel(animation: Animator) {}

            override fun onAnimationRepeat(animation: Animator) {}

        })
    }
}